package com.atlin.project.model.vo;

import cn.hutool.json.JSONUtil;
import com.atlin.beaconsapicommon.model.domain.interfaceInfo.InterfaceInfo;
import com.atlin.beaconsapicommon.model.domain.interfaceInfo.InvokeRequestField;
import com.atlin.beaconsapicommon.model.domain.interfaceInfo.RequestParamsField;
import com.atlin.beaconsapicommon.model.domain.interfaceInfo.ResponseParamsField;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 示例表
 *
 * @TableName interfaceInfo
 */
@Data
public class InterfaceInfoVO implements Serializable {


    /**
     * id
     */
    private Long id;

    /**
     * 接口名称
     */
    private String name;

    /**
     * 接口描述
     */
    private String description;

    /**
     * 接口主机地址
     */
    private String host;

    /**
     * 接口地址
     */
    private String url;

    /**
     * 请求头
     */
    private String requestHeader;

    /**
     * 响应头
     */
    private String responseHeader;

    /**
     * 请求类型
     */
    private String method;

    /**
     * 接口状态(0-关闭，1-开启)
     */
    private Integer status;

    /**
     * 创建用户ID
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 接口返回格式（JSON）
     */
    private String returnFormat;

    /**
     * 接口头像
     */
    private String avatarUrl;

    /**
     * 接口总调用次数
     */
    private Long totalInvoke;

    /**
     * 每次调用消耗积分数
     */
    private Long reduceScore;

    /**
     * 接口请求参数
     */
    private List<RequestParamsField> requestParams;    
    
    /**
     * 接口请求参数
     */
    private List<InvokeRequestField> requestParamsExample;

    /**
     * 接口响应参数
     */
    private List<ResponseParamsField> responseParams;


    /**
     * 包装类转对象
     *
     * @param interfaceInfoVO
     * @return
     */
    public static InterfaceInfo voToObj(InterfaceInfoVO interfaceInfoVO) {
        if (interfaceInfoVO == null) {
            return null;
        }
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        BeanUtils.copyProperties(interfaceInfoVO, interfaceInfo);
        List<ResponseParamsField> responseParamsFieldList = interfaceInfoVO.getResponseParams();

        if (!responseParamsFieldList.isEmpty()) {
            interfaceInfo.setResponseParams(JSONUtil.toJsonStr(responseParamsFieldList));
        }
        List<RequestParamsField> requestParamsFieldList = interfaceInfoVO.getRequestParams();
        if (!requestParamsFieldList.isEmpty()) {
            interfaceInfo.setRequestParams(JSONUtil.toJsonStr(requestParamsFieldList));
        }

        List<InvokeRequestField> requestParamsExampleFieldList = interfaceInfoVO.getRequestParamsExample();
        if (!requestParamsExampleFieldList.isEmpty()) {
            interfaceInfo.setRequestParamsExample(JSONUtil.toJsonStr(requestParamsExampleFieldList));
        }

        return interfaceInfo;
    }


    /**
     * 对象转包装类
     *
     * @param interfaceInfo
     * @return
     */
    public static InterfaceInfoVO objToVo(InterfaceInfo interfaceInfo) {
        if (interfaceInfo == null) {
            return null;
        }
        InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
        BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);

        String requestParamsFields = interfaceInfo.getRequestParams();
        String responseParamsFields = interfaceInfo.getResponseParams();
        String requestParamsExampleFields = interfaceInfo.getRequestParamsExample();
        if (StringUtils.isNotBlank(requestParamsFields)) {
            interfaceInfoVO.setRequestParams(JSONUtil.toList(requestParamsFields, RequestParamsField.class));
        }
        if (StringUtils.isNotBlank(responseParamsFields)) {
            interfaceInfoVO.setResponseParams(JSONUtil.toList(responseParamsFields, ResponseParamsField.class));
        }

        if (StringUtils.isNotBlank(requestParamsExampleFields)) {
            interfaceInfoVO.setRequestParamsExample(JSONUtil.toList(requestParamsExampleFields, InvokeRequestField.class));
        }

        return interfaceInfoVO;
    }

    private static final long serialVersionUID = 1L;
}